<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Zvit_contr extends CI_Controller {

	private $subjListDay1 = array();
	private $subjListDay2 = array();
	
	public function __construct() {
		parent::__construct();
		$tmp = new xmlSettings();
		if ($tmp->check_settings()) {
			if ($tmp->get_saId() == '6521B186-3E2B-441d-A09A-D0B5F47534B7') {
				$this->subjListDay1 = $tmp->get_subj_list('-1');
				$this->subjListDay2 = $tmp->get_subj_list('-2');
			}
		}
		
	}	

	public function index() {
		$this->load->view('obl_selection_form');			
	}
	
	public function  check_obl() {
		$this->load->library('form_validation');
		$this->form_validation->set_rules("obl", "Obl", "trim|required|exact_lehgth[2]|integer|xss_clean");
		if ($this->form_validation->run() == FALSE) {
			Logger::getInstance()->log(get_class($this), "Обнаружена ошибка, повторите выбор области!");
			$data['text'] = " - Обнаружена ошибка, повторите выбор области!";
			$this->load->view('obl_selection_form', $data);
		}
		else {
			$tmp = new xmlSettings();
			if ($tmp->check_settings()) {
				
				$data['saId'] = $tmp->get_saId();
				Logger::getInstance()->log(get_class($this), $data['saId']);
				$this->session;
				$this->session->set_userdata($data);
				$data['obl'] = $this->input->post('obl', true);
				$this->session->set_userdata($data);
				$data['daySubj'] = $tmp->get_subjects();
				$this->load->view('day_pt_selection_form', $data);	
			}
			else {
				Logger::getInstance()->log(get_class($this), "Ошибка в файле конфигурации");
				$this->load->view('obl_selection_form');
			}	
		
		}

	}
	
	public function pt_list(){
		if($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
		$day=$_GET['subj'];
		$arr = explode(";", $day); 
		$data['pt'] = $this->Zvit_model->get_pt_list($arr[0], $arr[1], $this->session->userdata('obl'), $this->session->userdata('saId'));
		header ("Content-Type:application/json");
		echo '[{"value":"","caption":"---"}';
		foreach ($data['pt'] as $tmp) {
			echo ',{"value":"'.$tmp['pId'].'","caption":"'.str_replace('"', "'", $tmp['eoName']).'"}';
		}
		echo "]";
		} else {
			Logger::getInstance()->log(get_class($this), "Что то пошло не так при формировании json");
		}
	}
	
	public function check_day_pt (){
		if ($this->session->userdata('saId')!='')
		{
			$data['pId'] = $this->input->post('pId');
			$arr = explode(";", $this->input->post('subj'));
			$data['subjId'] = $arr[0];
			$data['sesNum'] = $arr[1];
			$this->session->set_userdata($data);
			$ar['subjName'] = $this->Zvit_model->get_subj_name($data['subjId']);
			$ar['eoName'] = $this->Zvit_model->get_point_name($data['pId']);
			$ar['saId'] = $this->change_saId($this->session->userdata('saId'));
			$ar['obl'] = $this->session->userdata('obl');
			if ($ar['saId'] == '3E2B') {
				switch ($data['subjId']) {
					case '-1':
						$ar['subjList'] = $this->subjListDay1;
					break;
					case '-2':
						$ar['subjList'] = $this->subjListDay2;
					break;
				}
			}
			$this->load->view('stat_selection_form', $ar);		
		}
		else
		{
			Logger::getInstance()->log(get_class($this), "Сессия была уничтожена по истечении TTL");
			$data['text'] = " - Вы слишком долго бездействовали!";
			$this->load->view('obl_selection_form', $data);	
		}
	}

	public function check_stat(){
		if($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
			if ($this->session->userdata('saId') != '') {
			//Logger::getInstance()->log(get_class($this), $_POST['delivtime']."!!!!!!");
			$data['pId'] = $this->session->userdata('pId');
			$data['subjId'] = $this->session->userdata('subjId');
			$data['sesNum'] = $this->session->userdata('sesNum');
			$data['saId'] = $this->session->userdata('saId');
			switch ($data['saId']) {
				case '8006FC48-691A-4438-8176-AAAE9BCC90DB':
					foreach ($_POST as $key => $value) {
						$data[$key] = $value;
					}
					$this->Zvit_model->save_stat_data($data);
				break;
				case '6521B186-3E2B-441d-A09A-D0B5F47534B7':
					foreach ($_POST as $key => $value) {
						$tmp = strpos($key, 'subj');
						if ($tmp === 0) {
							$cncData[substr($key, 4)] = $value;
						} else {
							$data[$key] = $value;
						}
					}
					if (isset($cncData)) {
						$this->Zvit_model->save_stat_data($data);
						$this->Zvit_model->save_prob_stat_data($data, $cncData);
					} else {
						$this->Zvit_model->save_stat_data($data);
					}
				break;
			}
			
			
			} else {
				$arr['text'] = "Вы слишком долго бездействовали!";
				$this->load->view('obl_selection_form', $arr);
			}
		}
	}
	
	private function arrayKeyToStr ($arr) {
		foreach ($arr as $key => $value) {
			$tmp[] = $key;
		}
		return implode(',', $tmp);
	}
	
	public function stat_data_preload(){
		$str = '';
		$str1 = '';
		if($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
			$data['pId'] = $this->session->userdata('pId');
			$data['subjId'] = $this->session->userdata('subjId');
			$data['sesNum'] = $this->session->userdata('sesNum');
			$data['saId'] = $this->session->userdata('saId');
			if ($data['saId'] == '6521B186-3E2B-441d-A09A-D0B5F47534B7') {
				foreach ($_POST as $key => $value) {
					$tmp = strpos($key, 'subj');
					if ($tmp === 0) {
						$str1 .= substr($key, 4).',';
					} else {
						$str .= "o.$key, ";
					}
				}
				$fstr = substr($str, 0, strlen($str)-2);
				$fstr1 = substr($str1, 0, strlen($str1)-1);
				if (strlen($fstr1) > 0) {
					$arr = json_encode($this->Zvit_model->get_prob_stat_data($data, $fstr, $fstr1));
					header ("Content-Type:application/json");
					echo $arr;
				} else {
					$arr = json_encode($this->Zvit_model->get_stat_data($data, $fstr));
					header ("Content-Type:application/json");
					echo $arr;					
				}
				
			} elseif ($data['saId'] == '8006FC48-691A-4438-8176-AAAE9BCC90DB') {
				foreach ($_POST as $key => $value) {
					$str .= "o.$key, ";
				}
				$fstr = substr($str, 0, strlen($str)-2);
				$arr = json_encode($this->Zvit_model->get_stat_data($data, $fstr));
				header ("Content-Type:application/json");
				echo $arr;				
			}
		}
	}
	
	public function check_stat_filling(){
		$str = "";
		if($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
			foreach ($_POST as $key => $value) {
				$data[$key] = $value;
				switch ($value) {
					case "stat1":
						$str .= "o.delivkol,";
						break;
					case "stat2":
						$str .= "o.isready,";
						break;
					case "stat3":
						$str .= "o.sequrin,";
						break;
					case "stat4":
						$str .= "o.kolperson,";
						break;
					case "stat5":
						$str .= "o.kolnotcame,";
						break;
					case "stat6":
						$str .= "o.audend,";
						break;
					case "stat7":
						$str .= "o.kolgo,";
						break;
				}
			}
			$fstr = substr($str, 0, strlen($str)-1);
			$data['pId'] = $this->session->userdata('pId');
			$data['subjId'] = $this->session->userdata('subjId');
			$data['sesNum'] = $this->session->userdata('sesNum');
			$data['saId'] = $this->session->userdata('saId');
			$response = json_encode($this->Zvit_model->get_filling_inform($data, $fstr));
			header ("Content-Type:application/json");
			echo $response;
		}
	}

	public function stat_exit(){
		$this->session->sess_destroy();
		redirect('/', 'refresh');
	}
	
	private function change_saId ($saId) {
		switch ($saId) {
			case '6521B186-3E2B-441d-A09A-D0B5F47534B7':
				$resp = '3E2B';
			break;
			case '8006FC48-691A-4438-8176-AAAE9BCC90DB':
				$resp = '691A';
			break;
			case 'EA3C5EB1-819C-4EE1-A755-F27D3690430F':
				$resp = '819C';
			break;
			default:
				return 'Not valid saId!!!!';
			break;
		}
		return $resp;
	}
}